package gui;

import engine.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
 * Created by BKost on 19.06.2014.
 */
public class NewCreature {

    static JFrame frame;
    private static TextGUI gui;
    //public Cell cell;

    public NewCreature(World world) {
        this(world, null);
    }
    public NewCreature(World world, Cell cell) {
        if (cell != (null)){
            cellLabel.setVisible(false);
            cellXField.setVisible(false);
            cellYField.setVisible(false);

        }
        createPopulationButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < Integer.parseInt(NumberOfCreaturesField.getText()); i++) {
                    int width = Integer.parseInt(widthField.getText()) + Creature.random(Integer.parseInt(widthDispersionField.getText()));
                    int speed = Integer.parseInt(speedField.getText()) + Creature.random(Integer.parseInt(speedDispersionField.getText()));
                    int food = Integer.parseInt(foodNeedField.getText()) + Creature.random(Integer.parseInt(foodNeedDispersionField.getText()));
                    int stamina = Integer.parseInt(staminaField.getText()) + Creature.random(Integer.parseInt(staminaDispersionField.getText()));
                    SexType sex = ((Math.random() * 100 - Integer.parseInt(malePercentsField.getText())) < 0) ? SexType.male : SexType.female;
                    int maxEnergy = Integer.parseInt(maxEnergyField.getText()) + Creature.random(Integer.parseInt(maxEnergyDispersionField.getText()));
                    int energyRecovery = Integer.parseInt(energyRecoveryField.getText()) + Creature.random(Integer.parseInt(energyRecoveryDispersionField.getText()));
                    float vision = Float.parseFloat(visionField.getText()) + Creature.random(Float.parseFloat(visionDispersionField.getText()));
                    int colorRRand = Creature.random(Integer.parseInt(colorDispersionField.getText()));
                    int colorGRand = Creature.random(Integer.parseInt(colorDispersionField.getText()));
                    int colorBRand = Creature.random(Integer.parseInt(colorDispersionField.getText()));
                    Color color = Creature.colorConstr(Integer.parseInt(colorRField.getText()) + colorRRand,
                            Integer.parseInt(colorGField.getText()) + colorGRand,
                            Integer.parseInt(colorBField.getText()) + colorBRand);
                    int meatPrefer = Integer.parseInt(meatPreferField.getText()) + Creature.random(Integer.parseInt(meatPreferDispersionField.getText()));
                    int attack = Integer.parseInt(attackField.getText()) + Creature.random(Integer.parseInt(attackDispersionField.getText()));
                    int armor = Integer.parseInt(armorField.getText()) + Creature.random(Integer.parseInt(armorDispersionField.getText()));
                    Cell safeCell = cell;
                    if (safeCell == (null)) {
                        safeCell = world.field[Integer.parseInt(cellXField.getText()) * world.fieldWidth + Integer.parseInt(cellYField.getText())];
                    }
                    Creature creature = new Creature(safeCell, sex, speed, food, stamina, width,meatPrefer, color, maxEnergy, energyRecovery, vision, attack, armor);
                    safeCell.myWorld.creaturesOfWorld.add(creature);
                    safeCell.creaturesOfCell.add(creature);

                }
                gui.drawWorldPanel();
                gui.drawCellPanel();
                frame.dispose();

            }
        });
    }

    private JPanel NewCreatureMainPanel;
    private JTextField NumberOfCreaturesField;
    private JLabel sexLabel;
    private JTextField widthField;
    private JTextField speedField;
    private JTextField foodNeedField;
    private JTextField widthDispersionField;
    private JTextField speedDispersionField;
    private JTextField foodNeedDispersionField;
    private JCheckBox maleCheckBox;
    private JCheckBox femaleCheckBox;
    private JTextField malePercentsField;
    private JLabel NumberOfCreaturesLabel;
    private JPanel propertiesPanel;
    private JPanel numberOfCreaturesPanel;
    private JLabel malePercentsLabel;
    private JLabel widthLabel;
    private JLabel speedLabel;
    private JLabel foodNeedLabel;
    private JTextField staminaField;
    private JTextField staminaDispersionField;
    private JTextField colorDispersionField;
    private JLabel staminaLabel;
    private JLabel colorLabel;
    private JTextField colorRField;
    private JTextField colorGField;
    private JTextField colorBField;
    private JButton createPopulationButton;
    private JLabel cellLabel;
    private JTextField cellXField;
    private JTextField cellYField;
    private JLabel meatPreferLabel;
    private JTextField meatPreferField;
    private JTextField meatPreferDispersionField;
    private JLabel maxEnergyLabel;
    private JTextField maxEnergyField;
    private JTextField maxEnergyDispersionField;
    private JLabel energyRecoveryLabel;
    private JTextField energyRecoveryField;
    private JTextField energyRecoveryDispersionField;
    private JLabel visionLabel;
    private JTextField visionField;
    private JTextField visionDispersionField;
    private JLabel AttackLabel;
    private JTextField attackField;
    private JTextField attackDispersionField;
    private JLabel armorLabel;
    private JTextField armorField;
    private JTextField armorDispersionField;

    public static void start(Cell cell, TextGUI gui) {
        frame = new JFrame("NewCreature");
        frame.setContentPane(new NewCreature(cell.myWorld, cell).NewCreatureMainPanel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
        NewCreature.gui = gui;
    }

    public static void start(World world, TextGUI gui) {
        frame = new JFrame("NewCreature");
        frame.setContentPane(new NewCreature(world).NewCreatureMainPanel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
        NewCreature.gui = gui;
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here

    }
}
